use bevy::prelude::*;
use bevy_inspector_egui::{RegisterInspectable, InspectorPlugin, Inspectable};
use bevy_inspector_egui::widgets::InspectorQuery;
use crate::ui::UiPlugin;

mod ui;
mod components;
mod movement;

use components::*;
use crate::movement::MovementPlugin;

#[derive(Inspectable, Default)]
struct Data {
    query: InspectorQuery<Entity, With<Elevator>>,
    // buttons: InspectorQuery<Entity, With<Button>>,
}

#[derive(Default)]
struct CurrentFloor(i32); // 1 <= CurrentFloor <= Top floor

const WINDOW_WIDTH: f32 = 640.;
const WINDOW_HEIGHT: f32 = 480.;
const ELEVATOR_TOP_FLOOR: usize = 8;  // 电梯总层数

/// This example illustrates how to create a button that changes color and text based on its
/// interaction state.
fn main() {
    App::new()
        .insert_resource(WindowDescriptor {
            title: "Elevator Simulation".to_string(),
            width: WINDOW_WIDTH,
            height: WINDOW_HEIGHT,
            ..Default::default()
        })
        .add_plugins(DefaultPlugins)
        .add_plugin(UiPlugin)
        .add_plugin(ComponentsPlugin)
        .add_plugin(MovementPlugin)
        .add_plugin(InspectorPlugin::<Data>::new())
        // .add_plugin(WorldInspectorPlugin::new())
        .register_inspectable::<Elevator>()
        .add_system(debug_keys)
        .insert_resource(CurrentFloor(1))
        .run();
}


fn debug_keys(
    input: Res<Input<KeyCode>>,
    cursor: Res<CursorPosition>,
    mut floor: ResMut<CurrentFloor>,
) {
    if input.just_pressed(KeyCode::C) {
        println!("Cursor at x={}, y={}),", cursor.0.x, cursor.0.y);
    }
    if input.pressed(KeyCode::F) {
        floor.0 += 1;
    }
    if input.pressed(KeyCode::D) {
        floor.0 -= 1;
    }
}